/**
 * 差旅审核action类
 * @author 郑翎
 * 2015-10-1 11:07:43
 */
package com.cbts.action.info;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.cbts.action.travel.ApplayReimAction;
import com.cbts.entity.ApplayReim;
import com.cbts.entity.BusinessTravel;
import com.cbts.entity.BusinessTravelList;
import com.cbts.entity.Receipt;
import com.cbts.entity.UserInfo;
import com.cbts.service.info.UserInfoService;
import com.cbts.service.info.VerifierService;
import com.cbts.service.travel.ApplayReimService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VerifierAction extends ActionSupport {

	
	private ApplayReimAction applayReimAction;					//声明报销申请action
	private ApplayReimService applayReimService;				//报销记录的业务逻辑接口
	private VerifierService verifierService;					//差旅审核的业务逻辑接口
	private UserInfoService userInfoService;					// 用户信息的业务逻辑接口
	
	private List<BusinessTravelList> businessTravelLists;		//待审核状态的出差记录列表
	private List<BusinessTravelList> businessApplayReims;		//待报销状态的出差记录列表
	private List<BusinessTravel> businessTravels;				//出差详情
	private List<ApplayReim> applayReims;						//报销记录列表
	private List<Receipt> receipts;								//收据列表
	
	private ApplayReim applayReim;								//报销记录
	private UserInfo userInfo;									// 用户信息
	
	private String travelOrder;									//出差单号
	private String check; 										//checkbox值
	private BusinessTravelList businessTravelList;				//出差记录
	private String userid;										//获取session中的userid
	private String almessage;									//审核信息反馈
	private String travelstandrad;								//出差标准
	
	
	/**
	 *根据状态查询出差列表,map<travelOrder,applayreim> 
	 * @return getsuccess 跳转差旅审核页面
	 * @return geterror 获取异常，跳转到异常处理首页
	 */
	public String getTravelListByStatus() {
		try {
			ActionContext context = ActionContext.getContext();
			Map request = (Map) context.get("request");
			Map session=ServletActionContext.getContext().getSession();
			request.put("almessage", almessage);
			
			this.setAlmessage(null);
			
			businessApplayReims = new ArrayList<BusinessTravelList>();
			applayReims = new ArrayList<ApplayReim>();
			
			List<BusinessTravelList> busTralLists = verifierService
					.getBusinessTravelLists("待报销");
			if (!busTralLists.isEmpty()) {

				for (int i = 0; i < busTralLists.size(); i++) {
					
					ApplayReim tempaApplayReim = applayReimService.getApplayReim(busTralLists.get(i)
							.getTravelOrder());
					if ( tempaApplayReim!= null) {
						// 待报销的出差报销申请列表
						businessApplayReims.add(busTralLists.get(i));
						//待报销的出差记录
						applayReims.add(tempaApplayReim);
					}
				}
			} else {// 没有待审核的报销申请
				businessApplayReims = busTralLists;
			}
			businessTravelLists = verifierService.getBusinessTravelLists("待审核");
			if(businessTravelLists!=null){
				session.put("needCheck", businessTravelLists.size());
			}
			if(businessApplayReims!=null){
				session.put("needApplay", businessApplayReims.size());
			}
			return "getsuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "geterror";
		}
	}
	
	/**
	 * 根据出差单号获取出差列表和出差详情
	 * @return tssuccess 跳转到审核出差详情页面
	 * @return tserror 跳转到异常处理页面
	 */
	public String getTravelsByOrder() {
		try {
			String[] temp = travelOrder.split("-");
			if (temp!=null&&temp.length>1) {
				userInfo = userInfoService.getUserInfo(temp[temp.length-1]);
				if (userInfo!=null&&userInfo.getUserGroup()!=null
						&&userInfo.getUserGroup().getTravelStandard()!=null) {
					travelstandrad = userInfo.getUserid()+"所在的"+userInfo.getUserGroup().getUserGroupName()
							+"的出差标准如下:\n"+"交通:"+userInfo.getUserGroup().getTravelStandard().getTraffic().getTraffic()
							+","+userInfo.getUserGroup().getTravelStandard().getTraffic().getStandard()
							+"  "+userInfo.getUserGroup().getTravelStandard().getTraffic().getPrice()+"元/公里\n"
							+"住宿:"+userInfo.getUserGroup().getTravelStandard().getHotel().getHotel()
							+"  "+userInfo.getUserGroup().getTravelStandard().getHotel().getPrice()+"元/天\n"
							+"餐饮费:"+userInfo.getUserGroup().getTravelStandard().getMealsCost()+"元/天\n"
							+"其它费用:"+userInfo.getUserGroup().getTravelStandard().getElseCost()+"元/天";
				}else {
					travelstandrad = "该职工不属于任何用户组，或者\n该职工所在的用户组不具备出差条件";
				}
			}
			// 根据出差单号获取出差列表
			businessTravelList = applayReimService
					.getTravelListByOrder(travelOrder);
			// 根据出差单号获取出差信息列表
			businessTravels = applayReimService.getTravelInfoList(travelOrder);
			
			return "tssuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "tserror";
		}
	}

	/**
	 * 根据出差单号获取具体的出差信息列表和收据
	 * @return SUCCESS 跳转到审核报销详情页面
	 * @return ERROR 跳转到异常处理页面
	 */
	public String getTravelInfoList() {
		try {
			
			String[] tempuser = travelOrder.split("-");
			if (tempuser!=null&&tempuser.length>1) {
				userInfo = userInfoService.getUserInfo(tempuser[tempuser.length-1]);
				if (userInfo!=null&&userInfo.getUserGroup()!=null
						&&userInfo.getUserGroup().getTravelStandard()!=null) {
					travelstandrad = userInfo.getUserid()+"所在的"+userInfo.getUserGroup().getUserGroupName()
							+"的出差标准如下:\n"+"交通:"+userInfo.getUserGroup().getTravelStandard().getTraffic().getTraffic()
							+","+userInfo.getUserGroup().getTravelStandard().getTraffic().getStandard()
							+"  "+userInfo.getUserGroup().getTravelStandard().getTraffic().getPrice()+"元/公里\n"
							+"住宿:"+userInfo.getUserGroup().getTravelStandard().getHotel().getHotel()
							+"  "+userInfo.getUserGroup().getTravelStandard().getHotel().getPrice()+"元/天\n"
							+"餐饮费:"+userInfo.getUserGroup().getTravelStandard().getMealsCost()+"元/天\n"
							+"其它费用:"+userInfo.getUserGroup().getTravelStandard().getElseCost()+"元/天";
					System.err.println(travelstandrad);
				}else {
					travelstandrad = "该职工不属于任何用户组，或者\n该职工所在的用户组不具备出差条件";
				}
			}
			// 根据出差单号获取出差列表
			businessTravelList = applayReimService
					.getTravelListByOrder(travelOrder);
			// 根据出差单号获取出差信息列表
			businessTravels = applayReimService.getTravelInfoList(travelOrder);
			// 根据出差单号获得报销信息
			applayReim = applayReimService.getApplayReim(travelOrder);
			// 根据出差单号获得收据列表
			receipts = applayReimService.getReceiptFile(travelOrder);
			if (travelOrder!=null) {
				String[] temp = travelOrder.split("-");
				userid = temp[temp.length-1];
				applayReimAction.dowLoadList(receipts,userid);
			}
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 根据出差单号，修改该出差列表的状态
	 * @return alsuccess 跳转到差旅审核页面
	 * @return alerror 跳转到异常处理页面
	 */
	public String alterStatus() {
		try {
			almessage = verifierService.alterStatus(travelOrder, businessTravelList.getVerifierStatus(),applayReim,this.getUserid());
	
			return "alsuccess";
		
		} catch (Exception e) {
			e.printStackTrace();
			return "alerror";
		}
	}

	/**
	 * 批量修改出差列表状态
	 * @return alsuccess 跳转到差旅审核页面
	 * @return alerror 跳转到异常处理页面
	 */
	public String alterListByStatus() {
		
		
		if (this.getCheck()!=null) {
			try {
				String ids = this.getCheck();
				String[] arraycheckbox = ids.split(", ");
				//出差审核
				if (businessTravelLists!=null&&!businessTravelLists.isEmpty()&&businessTravelLists.size()!=0) {
					
					for (int i = 0; i < businessTravelLists.size(); i++) {
						
						BusinessTravelList tempEntity = businessTravelLists.get(i);
						
						for (int j = 0; j < arraycheckbox.length; j++) {
							if (tempEntity.getTravelOrder().equals(arraycheckbox[j])) {
								
								almessage = verifierService.alterStatus(tempEntity.getTravelOrder()
										,tempEntity.getVerifierStatus(),null,this.getUserid());
								
							}
							
						}
					}
				}
				//报销审核
				if (businessApplayReims!=null&&!businessApplayReims.isEmpty()&&businessApplayReims.size()!=0) {
					
					for (int i = 0; i < businessApplayReims.size(); i++) {
						
						BusinessTravelList tempEntity = businessApplayReims.get(i);
						
						for (int j = 0; j < arraycheckbox.length; j++) {
							if (tempEntity.getTravelOrder().equals(arraycheckbox[j])) {
								if (tempEntity.getVerifierStatus().equals("已报销")) {
									
									ApplayReim applayReim = new ApplayReim();
									applayReim.setRealAppAmount(tempEntity.getApplayReim().getRealAppAmount());
									applayReim.setSaveCost(tempEntity.getApplayReim().getSaveCost());
									
									almessage = verifierService.alterStatus(tempEntity.getTravelOrder()
											,tempEntity.getVerifierStatus(),applayReim,this.getUserid());
								}
								else {
									
									almessage = verifierService.alterStatus(tempEntity.getTravelOrder()
											,tempEntity.getVerifierStatus(),null,this.getUserid());
								}
								
							}
							
						}
					}
				}
				return "alsuccess";
				
			} catch (Exception e) {
				return "alerror";
			}
		}
		return "alerror";
	}

	public List<BusinessTravelList> getBusinessTravelLists() {
		return businessTravelLists;
	}

	public void setBusinessTravelLists(
			List<BusinessTravelList> businessTravelLists) {
		this.businessTravelLists = businessTravelLists;
	}

	public List<BusinessTravel> getBusinessTravels() {
		return businessTravels;
	}

	public void setBusinessTravels(List<BusinessTravel> businessTravels) {
		this.businessTravels = businessTravels;
	}

	public ApplayReim getApplayReim() {
		return applayReim;
	}

	public void setApplayReim(ApplayReim applayReim) {
		this.applayReim = applayReim;
	}

	public List<Receipt> getReceipts() {
		return receipts;
	}

	public void setReceipts(List<Receipt> receipts) {
		this.receipts = receipts;
	}

	public String getTravelOrder() {
		return travelOrder;
	}

	public void setTravelOrder(String travelOrder) {
		this.travelOrder = travelOrder;
	}

	public BusinessTravelList getBusinessTravelList() {
		return businessTravelList;
	}

	public void setBusinessTravelList(BusinessTravelList businessTravelList) {
		this.businessTravelList = businessTravelList;
	}

	public ApplayReimService getApplayReimService() {
		return applayReimService;
	}

	public void setApplayReimService(ApplayReimService applayReimService) {
		this.applayReimService = applayReimService;
	}

	public VerifierService getVerifierService() {
		return verifierService;
	}

	public void setVerifierService(VerifierService verifierService) {
		this.verifierService = verifierService;
	}

	public List<ApplayReim> getApplayReims() {
		return applayReims;
	}

	public void setApplayReims(List<ApplayReim> applayReims) {
		this.applayReims = applayReims;
	}

	public List<BusinessTravelList> getBusinessApplayReims() {
		return businessApplayReims;
	}

	public void setBusinessApplayReims(List<BusinessTravelList> businessApplayReims) {
		this.businessApplayReims = businessApplayReims;
	}

	public ApplayReimAction getApplayReimAction() {
		return applayReimAction;
	}

	public void setApplayReimAction(ApplayReimAction applayReimAction) {
		this.applayReimAction = applayReimAction;
	}

	
	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public String getUserid() throws Exception{
		try {
			userid = ServletActionContext.getContext().getSession().get("userid").toString();
		} catch (Exception e) {
			throw e;
		}
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getAlmessage() {
		
		return almessage;
	}

	public void setAlmessage(String almessage) {
		this.almessage = almessage;
	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	public String getTravelstandrad() {
		return travelstandrad;
	}
	public void setTravelstandrad(String travelstandrad) {
		this.travelstandrad = travelstandrad;
	}
	
}
